package com.ie.sevice.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.comment.http.ResponseResult;
import com.ie.comment.sercurity.utiles.ConfigrarionBean;
import com.ie.comment.sercurity.utiles.JwtTokenUtil;
import com.ie.comment.sercurity.utiles.RedisUtil;
import com.ie.entity.AUserEntity;
import com.ie.mapper.admin.AUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hp
 */
@Service
public class AUserServiceImpl extends ServiceImpl<AUserMapper,AUserEntity> implements AUserService {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ConfigrarionBean config;


    //对传入的对象参数进行比较
      //1、有这个用户，a、密码正确，b、密码错误
      //无此用户，返回
      //2、密码正确，返回token，跳转到相应页面

    @Override
    public ResponseResult<Map<String, String>> login(AUserEntity aUserEntity) {
        //获取这个参数
        Long count = lambdaQuery().eq(AUserEntity::getAname,aUserEntity.getAname()).count();
        if (count == 0){
            return ResponseResult.getMessageResult(null,"A001");
        }
        HashMap<String , String> mapUser = new HashMap<>();
        mapUser.put("aname",aUserEntity.getAname());
        mapUser.put("apwd",aUserEntity.getApwd());
        List<AUserEntity> userList = query().allEq(mapUser).list();
        if (userList.size() > 0){
           String token = jwtTokenUtil.createToken(aUserEntity.getAname());
           redisUtil.set("login_"+aUserEntity.getAname(), aUserEntity.getAname() , config.getRedisExpiration());
           HashMap<String , String> loginresult = new HashMap<>();
           loginresult.put("authtoken",token);
           loginresult.put("aname",aUserEntity.getAname());
           loginresult.put("aid",aUserEntity.getId()+"");
           return ResponseResult.getSuccessResult(loginresult);
        }else {
            return ResponseResult.getMessageResult(null,"A001");
        }

        
    }
}
